﻿using FunArts.EasyDevOps.Rcl.Service;
using FunArts.EasyDevOps.Rcl.ViewModels;
using FunArts.EasyDevOps.Shared.Models.Enums;
using Masa.Blazor;
using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunArts.EasyDevOps.Rcl.Shared
{
    public partial class MainLayout
    {
        [Inject]
        public ISshClientService SshClientService { get; set; }
        /// <summary>
        /// 菜单是否展开
        /// </summary>
        private bool? _drawer;
        /// <summary>
        /// 菜单
        /// </summary>
        private List<NavItemViewModel> _navItems = new();
        /// <summary>
        /// 连接信息
        /// </summary>
        private ConnectInfoViewModel _connectInfoViewModel = new()
        {
            //SystemType=SystemTypeEnum.Linux,
            //Host="10.0.0.148",
            //User="root",
            //Pwd= "Rjsoft@2024-hsr",
            //虚拟机contos
            //SystemType = SystemTypeEnum.Linux,
            //Host = "172.24.230.88",
            //User = "root",
            //Pwd = "hyf1994",
            //虚拟机ubuntu
            SystemType = SystemTypeEnum.Linux,
            Host = "172.19.215.194",
            User = "root",
            Pwd = "hyf19940129",
        };
        /// <summary>
        /// 系统类型
        /// </summary>
        private List<SystemTypeEnum> _systemTypeEnumList = Enum.GetValues(typeof(SystemTypeEnum))
                                                          .Cast<SystemTypeEnum>()
                                                          .ToList();
        /// <summary>
        /// 密码是否可见
        /// </summary>
        private bool _isPasswordVisible { get; set; } = false;
        /// <summary>
        /// 连接状态
        /// </summary>
        public bool IsConnecting { get; set; }=false;
        private string ConnectErrorMsg =string.Empty;
        /// <summary>
        /// 校验触发时机
        /// </summary>
        private ValidateOn _validateOn;
        /// <summary>
        /// 密码可见状态改变
        /// </summary>
        private void TogglePasswordVisibility()
        {
            _isPasswordVisible = !_isPasswordVisible;
        }
        /// <summary>
        /// 连接到服务器
        /// </summary>
        private async void ConnectService()
        {
            IsConnecting = true;
            ConnectErrorMsg = string.Empty;
            var connectResult=await Task.Run(() =>
            {
                SshClientService.SshClientConnectInfo = new EasyDevOps.Shared.Models.SshClient.SshClientConnectInfo()
                {
                    SystemType = _connectInfoViewModel.SystemType,
                    Host = _connectInfoViewModel.Host,
                    User = _connectInfoViewModel.User,
                    Pwd = _connectInfoViewModel.Pwd
                };
                return SshClientService.Connect(out ConnectErrorMsg);
            });
            
            IsConnecting = false;
            StateHasChanged();
        }

        private void CloseConnect()
        {
            SshClientService.Disconnect();
        }

        protected override void OnAfterRender(bool firstRender)
        {
            base.OnAfterRender(firstRender);

            if (firstRender)
            {
                _navItems = new List<NavItemViewModel>()
                {
                    new NavItemViewModel("主页", "/"),
                    new NavItemViewModel("基础设施", "mdi-monitor", new List<NavItemViewModel>()
                    {
                        new NavItemViewModel("总览","/SystemPage"),
                        new NavItemViewModel("安装应用","/InstallApp"),
                        new NavItemViewModel("进程信息","/PorcessInfo"),
                        new NavItemViewModel("Nginx管理","/NginxInfo"),
                        new NavItemViewModel("KeepAlived管理","/keepalivedConfig"),
                    }),
                };
                StateHasChanged();
            }
        }
    }
}
